Installing node with nvm
Installing node with nvm
Download the install script
Using curl, or wget, download the installation script. In the URL below make sure you replace v0.39.2
with the latest version of nvm.
curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh
It's not a bad idea to open the install script and inspect its contents given that you just downloaded it from the Internet.
Run the install script
Run the install script with bash
.
bash install_nvm.sh
This script clones the nvm repository into ~/.nvm. Then it updates your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc) to source the nvm.sh it contains.
You can confirm that your profile is updated by looking at the install script's output to determine which file it used. Look for something like the following in that file:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Restart your terminal
In order to pick up the changes to your profile either close and reopen the terminal, or manually source your respective ~/.profile.
Example:
source ~/.bash_profile
Verify it worked
Finally, you can verify that it's installed with the command
command:
command -v nvm
Should return nvm
. Note: You can't use the which
command with nvm
since it's a shell function and not an actual application.
See what it does
Finally, run the nvm
command to get a list of all the available sub-commands and to further verify that installation worked.
Use nvm to install the latest LTS release of Node.js
Now that you've got nvm installed let's use it to install, and use, the current LTS version of Node.js.
nvm install --lts
# Output
Installing latest LTS version.
Downloading and installing node v10.16.3...
Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-darwin-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.16.3 (npm v6.9.0)
Creating default alias: default -> lts/* (-> v10.16.3)
Verify it worked, and that the version is correct:
node --version
# => v10.16.3
which node
# => /Users/joe/.nvm/versions/node/v10.16.3/bin/node
Note this line Creating default alias: default -> lts/* (-> v10.16.3)
. This indicates that nvm has set lts/*
as the default alias. Practically this means that anytime you start a new shell, and the nvm.sh script is sourced, it will default that shell to using the installed lts release. You can change this behavior using the nvm alias
command.
Example to set the default version of node to use when starting a new shell to 10.0.0:
nvm alias default 10.0.0
Use nvm to install other versions of Node.js
The real benefit of nvm comes when you install different versions of Node.js. You can then switch between them depending on which project you're working on.
List available versions
To see the entire list of Node.js versions available to install, enter the following:
nvm ls-remote
Install a specific version
Install a specific version:
nvm install 8.16.2
Install the latest release:
nvm install node
Install an older LTS release by codename:
nvm install carbon
# => Installs v8.16.2 the latest release of the Carbon LTS line.
List installed versions
You can see which versions of Node.js you have installed already, and therefore which ones you can use with the nvm ls
command:
nvm ls
This will output a list of installed versions, as well as indicate which version is currently being used by the active shell.
Switch to another version
To switch to another version for the active shell use nvm use
.
For a specific version provide a version number:
nvm use 10.16.3
# => Now using node v10.16.3 (npm v6.9.0)
Switch to the latest installed version:
nvm use node
Use the latest LTS version:
nvm use --lts
Tip: Use nvm alias default {VERSION}
to switch the version of Node.js used by default when starting a new shell.
Newsletter
If you liked this post, sign up to get updates in your email when I write something new! No spam ever.
Subscribe to the Newsletter